package com.controller;

import com.jdlm.JdlmService;
import com.req.ChangeReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/jdlm")
public class JdlmController {

    @Autowired
    private JdlmService jdlmService;

    @PostMapping(value = "/change")
    public Map<String,String> change(@RequestBody ChangeReq changeReq){
        if(!StringUtils.hasText(changeReq.getUrl())){
            return error();
        }
        String s = jdlmService.startShibie(changeReq.getUrl());
        return result(0,"请求成功",s);
    }

    private Map<String, String> error() {
        return result(101,"找不到链接", null);
    }

    private Map<String, String> result(int i, String msg, String s) {
        Map<String,String> map = new HashMap<>();
        map.put("code",i+"");
        map.put("msg",msg);
        map.put("data",s);
        return map;
    }
}
